<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="患者编号" prop="patientNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.patientNo"-->
<!--          placeholder="请输入患者编号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery" -->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="患者名称" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入患者名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="patientIdentityNo">
        <el-input
          v-model="queryParams.patientIdentityNo"
          placeholder="请输入患者身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="患者状态" prop="patientStatus">
        <el-select v-model="queryParams.patientStatus" placeholder="请选择患者状态" clearable>
          <el-option
            v-for="dict in dict.type.patient_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:patient:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:patient:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="patientList" @selection-change="handleSelectionChange" :default-sort = "{prop: 'patientNo', order: 'descending'}">
      <el-table-column label="序号" type="index" align="center" width="50"/>
      <el-table-column label="患者身份证号" align="center" prop="patientIdentityNo"/>
      <el-table-column label="患者名称" align="center" prop="patientName"/>
      <el-table-column label="患者年龄" align="center" prop="patientAge"/>
      <el-table-column label="患者性别" align="center" prop="patientGender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.patientGender"/>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" align="center" prop="patientPhone"></el-table-column>
      <el-table-column label="患者状态" align="center" prop="patientStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.patient_status" :value="scope.row.patientStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:patient:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-folder"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:patient:detail']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:patient:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改患者对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-form-item label="患者编号" prop="patientNo">
          <el-input v-model="form.patientNo" placeholder="请输入患者编号"/>
        </el-form-item>
        <el-form-item label="患者名称" prop="patientName">
          <el-input v-model="form.patientName" placeholder="请输入患者名称"/>
        </el-form-item>
        <el-form-item label="患者生日" prop="patientBirthday">
          <el-date-picker clearable
                          v-model="form.patientBirthday"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择患者生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="患者年龄" prop="patientAge">
          <el-input v-model="form.patientAge" placeholder="请输入患者年龄" />
        </el-form-item>
        <el-form-item label="患者性别" prop="patientGender">
          <el-select v-model="form.patientGender" placeholder="请选择患者性别" clearable>
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
<!--          <el-input v-model="form.patientGender" placeholder="请输入患者性别"/>-->
        </el-form-item>
        <el-form-item label="患者身份证号" prop="patientIdentityNo">
          <el-input v-model="form.patientIdentityNo" placeholder="请输入患者身份证号"/>
        </el-form-item>
        <el-form-item label="患者职业" prop="patientJob">
          <el-input v-model="form.patientJob" placeholder="请输入患者职业"/>
        </el-form-item>
        <el-form-item label="患者体重" prop="patientWeight">
          <el-input v-model="form.patientWeight" placeholder="请输入患者体重"/>
        </el-form-item>
        <el-form-item label="患者身高" prop="patientHeight">
          <el-input v-model="form.patientHeight" placeholder="请输入患者身高"/>
        </el-form-item>
        <el-form-item label="患者民族" prop="patientFolk">
          <el-input v-model="form.patientFolk" placeholder="请输入患者民族"/>
        </el-form-item>
        <el-form-item label="患者地址" prop="patientAddress">
          <el-input v-model="form.patientAddress" placeholder="请输入患者地址"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="patientPhone">
          <el-input v-model="form.patientPhone" placeholder="请输入联系方式"/>
        </el-form-item>
        <el-form-item label="婚姻状态" prop="patientMarriage">
            <el-select v-model="form.patientMarriage" placeholder="请选择患者状态" clearable>
              <el-option
                v-for="dict in dict.type.marital_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>

        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listPatient, getPatient, delPatient, addPatient, updatePatient} from "@/api/gaoxin/patient";

export default {
  name: "Patient",
  dicts: ['patient_status','sys_user_sex','marital_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 患者表格数据
      patientList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientNo: null,
        patientName: null,
        patientBirthday: null,
        patientAge: null,
        patientGender: null,
        patientIdentityNo: null,
        patientJob: null,
        patientWeight: null,
        patientHeight: null,
        patientFolk: null,
        patientAddress: null,
        patientPhone: null,
        patientMarriage: null,
        patientStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        patientNo: [
          {
            required: true,
            message: "患者编号不能为空",
            trigger: "blur",
          },
          {
            pattern:/^[1-9]\d*$/,
            message: "编号只能为正整数数字",
            trigger: "blur"
          }
        ],
        patientName: [
          {
            required: true,
            message: "患者姓名不能为空",
            trigger: "blur"
          },
        ],
        patientBirthday: [
          {
            required: true,
            message: '请选择当前日期',
            trigger: 'change'
          }
        ],
        patientAge: [
          {
            required: true,
            message: "请输入患者年龄",
            trigger: "blur"
          },
          {
            pattern:/^[1-9]\d*$/,
            message: '年龄必须为数字',
            trigger: 'blur'
          }
        ],
        patientGender: [
          {
            required: true,
            message: '请输入患者性别',
            trigger: "blur"
          }
        ],
        patientIdentityNo: [
          {
            required: true,
            message: '身份证号不能为空',
            trigger: 'blur'
          },
          {
            pattern:/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
            message: "请输入正确的身份证号",
            trigger: 'blur',
          }
        ],
        patientPhone: [
          {
            required: true,
            message: '联系方式不能为空',
            trigger: 'blur'
          },
          {
            pattern:/^((0\d{2,3}-\d{7,8})|(1[34578]\d{9}))$/,
            message: "请输入正确的手机号或固话",
            trigger: "blur",
          }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("marital_status").then(res=>{
      this.marital_status=res.data;
    })
  },
  methods: {
    /** 查询患者列表 */
    getList() {
      this.loading = true;
      listPatient(this.queryParams).then(response => {
        this.patientList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        patientId: null,
        patientNo: null,
        patientName: null,
        patientBirthday: null,
        patientAge: null,
        patientGender: null,
        patientIdentityNo: null,
        patientJob: null,
        patientWeight: null,
        patientHeight: null,
        patientFolk: null,
        patientAddress: null,
        patientPhone: null,
        patientMarriage: null,
        patientStatus: "0",
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      let patientId = row.patientId;
      this.$router.push("/patient/detail/" + patientId);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.patientId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加患者";
      this.$router.push("/patient/add");

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const name = row.patientName;
      const status = row.patientStatus;
      if(status === '1') {
        this.$message.warning(`患者未就诊，无法修改`);
        return
      }
      let patientId = row.patientId;
      this.$router.push("/patient/update/" + patientId);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.patientId != null) {
            updatePatient(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPatient(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              console.log(response)
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const patientIds = row.patientId || this.ids;
      const patientName = row.patientName;
      this.$modal.confirm(`是否删除患者"${patientName}"的数据项？`).then(function () {
        return delPatient(patientIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/patient/export', {
        ...this.queryParams
      }, `patient_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
